#include "Characters/Farmer.h"
#include "../States/PatrolState.h"
#include "../Utils/FileUtils.h"

CFarmer::CFarmer()
: CCharacter(CharacterType::Farmer)
{
	m_tFarmerTex.loadFromFile("Resources/Art/Characters/Farmer.png");
	m_tFarmer.setTexture(m_tFarmerTex);

	m_tAnimation = new CAnimation();
	m_tAnimation->SetupAnimation(m_tFarmer, 4, 1, 0.075f);
	

	m_pStateMachine->SetCurrentState(PATROL);

	Json::Value root = FileUtils::ParseJsonFromFile("Resources/Characters/farmer_routes.json");

	if (root.empty())
	{
		DebugAssert(false, "Empty root json");
		return;
	}

	for (auto it = root.begin(); it != root.end(); ++it)
	{
		std::vector<sf::Vector2f>& v = m_tRoutes[FarmerState::EnumForString[(*it)["route_name"].asString()]];
		for (auto it2 = (*it)["route"].begin(); it2 != (*it)["route"].end(); ++it2)
		{
			sf::Vector2f vf((float)(*it2)["x"].asDouble(), (float)(*it2)["y"].asDouble());
			v.push_back(vf);
		}
	}

	SetState(FarmerState::PatrollingChickenPen);
}

CFarmer::~CFarmer()
{
}

void CFarmer::SetState(FarmerState::Enum state)
{
	PATROL->SetRoute(m_tRoutes[state]);
}

void CFarmer::DoMoveUp(float dt)
{
	m_vPosition.y -= GetMovementSpeed() * dt;
}

void CFarmer::DoMoveDown(float dt)
{
	m_vPosition.y += GetMovementSpeed() * dt;
}

void CFarmer::DoMoveLeft(float dt)
{
	m_vPosition.x -= GetMovementSpeed() * dt;
}

void CFarmer::DoMoveRight(float dt)
{
	m_vPosition.x += GetMovementSpeed() * dt;
}

void CFarmer::DoUseSkill()
{
}

void CFarmer::DoUpdate(float dt)
{
	m_tAnimation->SetIdle(false);
	m_tAnimation->Process(dt);
}

void CFarmer::DoRender(sf::RenderWindow* pWin)
{
	m_tAnimation->SetPosition(m_vPosition);
	m_tAnimation->Render(pWin);
}

void CFarmer::Idle()
{
	m_tAnimation->SetIdle(false);
}